package com.gljx.web.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.management.relation.RoleList;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gljx.constant.Constant;
import com.gljx.shiro.passwordEncrypt.PasswordHelper;
import com.gljx.util.CommonUtil;
import com.gljx.web.dao.RoleMapper;
import com.gljx.web.Entity.Role;
import com.gljx.web.Entity.User;
import com.gljx.web.service.RoleService;

@Service
@Transactional
public class RoleServiceImpl implements RoleService{

	@Autowired
	private RoleMapper roleDao;
	
	@Override
	public Map<String, Object> selectRoleByProject(String rows, 
											String page, 
											String roleName, 
											String fisDelete) {
		Integer pageNum = Integer.valueOf(page);
		Integer pageSize = Integer.valueOf(rows);
		Map <String,Object > data = new HashMap<String, Object>();
		data.put("Pindex", pageNum);
		String sql=  "select ID, ROLE_NAME, CREAT_TIME, UPDATE_TIME,"
				+ " CREAT_ID, UPDATE_ID, REMARK, FIS_DELETE,  "
				+ "PROJECTNUMBER, ROLE_CODE from t_e_role "
				+ "where projectnumber='"+Constant.Project_ID+"'";
		if(roleName != null && !"".equals(roleName)){
			sql+= " and ROLE_NAME like '%"+roleName+"%'" ;//角色名
		}
		if(!"no".equals(fisDelete)){
			sql+= " and FIS_DELETE = "+fisDelete;//姓名
		}
		data.put("Psql",sql);
		data.put("Psize", pageSize);
		
		roleDao.selectRoleByProject(data);
		return data;
	}

	@Override
	public Integer insertRole(Role role) throws Exception{
		
		return roleDao.insertSelective(role);
	}

	@Override
	public Map<String, Object> insertRole(String v_role_name, 
										  String v_role_code, 
										  String v_creat_time,
										  String v_creat_id, 
										  String v_remark, 
										  Integer v_fisDelete) throws Exception{
		Map <String,Object > data = new HashMap<String, Object>();
		data.put("v_role_name", v_role_name);
		data.put("v_role_code", v_role_code);
		data.put("v_creat_time", v_creat_time);
		data.put("v_creat_id", v_creat_id);
		data.put("v_remark", v_remark);
		data.put("v_projectnumber", Constant.Project_ID);
		roleDao.insertRole(data);
		
		return data;
	}

	@Override
	public Role selectByPrimaryKey(Integer id) {

		return roleDao.selectByPrimaryKey(id);
	}

	@Override
	public Integer updateByPrimaryKeySelective(Role role) throws Exception{
		role.setProjectnumber(Constant.Project_ID);
		role.setUpdateId((String)SecurityUtils.getSubject().getPrincipal());
		role.setUpdateTime(CommonUtil.nowTime());
		return roleDao.updateByPrimaryKeySelective(role);
	}

	@Override
	public Integer setdelete(String ids, String isDelete) throws Exception {
		String[] idStrings = ids.split(",");
		List<Role> roles = new ArrayList<Role>();
		String updateId = (String)SecurityUtils.getSubject().getPrincipal();
		//处理传入参数
		for(String string : idStrings){
			Role role = new Role();
			//启用停用标记
			role.setFisDelete(Integer.valueOf(isDelete));
			//修改人修改时间
			role.setUpdateId(updateId);
			role.setUpdateTime(CommonUtil.nowTime());
			//id
			role.setId(Integer.valueOf(string));
			roles.add(role);
		}
		return roleDao.updateRoleDelete(roles);
	}

	@Override
	public Map<String, Object> selectRoleInPower(String rows, 
												 String page, 
												 String roleName,
												 String userId) {
		Integer pageNum = Integer.valueOf(page);
		Integer pageSize = Integer.valueOf(rows);
		Map <String,Object > data = new HashMap<String, Object>();
		data.put("Pindex", pageNum);
		String sql=  "select r.ID, r.ROLE_NAME, r.REMARK,r.ROLE_CODE "
				+ "from t_e_role r "
				+ "where r.projectnumber='"+Constant.Project_ID+"' and r.fis_delete=0 ";
		if(roleName != null && !"".equals(roleName)){
			sql+= " and r.ROLE_NAME like '%"+roleName+"%'" ;//角色名
		}
		
		String sqlBef ="minus "
				+ "select r.ID, r.ROLE_NAME, r.REMARK,r.ROLE_CODE "
				+ "from t_e_role r "
					+ "inner join t_e_role_user ru on ru.role_id = r.id "
				+ "where r.projectnumber='"+Constant.Project_ID+"' "
				+ "and r.fis_delete=0 and ru.user_id="+Integer.valueOf(userId);
				
		sql +=sqlBef;
		data.put("Psql",sql);
		data.put("Psize", pageSize);
		
		roleDao.selectRoleByProject(data);
		return data;
	}

	@Override
	public List<Map<String, Object>> findHandle(Integer id, String menuName, String PID, String name) {
		return roleDao.findHandle(id, menuName, PID, name);
	}

}
